//
//  CZTabBar.swift
//  Weibo
//
//  Created by 董松 on 16/2/26.
//  Copyright © 2016年 董松. All rights reserved.
//

import UIKit

/*
如何自定义
1.修改TabBar里子控件的frame
2.修改frame之后可以添加一个button
*/

class CZTabBar: UITabBar {
    
    //自定义视图
    //第一步：重写init
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加视图和设置约束
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //第二步：创建一个私有方法，来添加视图和设置约束
    private func setupUI(){
        addSubview(composeButton)
    }
    
    //第三步：懒加载
    lazy var composeButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        //设置按钮的属性
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //可以添加点击事件
        //设置大小
        button.sizeToFit()
        
        return button
        
    }()
    
    //在这里去修改
    override func layoutSubviews() {
        //先调用super.layoutSubviews
        super.layoutSubviews()
        //创建一个索引
        var index = 0
        let width = bounds.width / 5
        let height = bounds.height
        
        for subview in subviews{
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                //修改frame
                subview.frame = CGRectMake(CGFloat (index++) * width, 0, width, height)
                //判断一下索引
                if index == 2 {index++}
            }
        }
        
        //设置按钮的位置
        composeButton.frame = CGRectMake(2 * width, 0, width, height)
    }
    
    
    
    
    
}
